using UnityEngine;
using System.Collections;
using UnityEditor;

public static class Addons
{
	[MenuItem ("Addons/Create Path Map")]
	public static void newPathMap ()
	{
		GameObject mapHost = new GameObject ("mapHost");
		PathMapBehaviour pathMapController = mapHost.AddComponent<PathMapBehaviour> ();
		pathMapController.pathMap = new PathMap ();
		
		Selection.activeGameObject = mapHost;
	}
	
	[MenuItem ("Addons/Create Path Joint")]
	public static void newPathJoint ()
	{
		PathMapBehaviour pathMapController = null;
		if (Selection.activeObject != null)
		{
			pathMapController = Selection.activeGameObject.GetComponent<PathMapBehaviour> ();
		}
		
		if (pathMapController == null)
		{
			Debug.LogError ("Please select a PathMap first!");
			return;
		}
		
		GameObject jointHost = new GameObject ("jointHost");
		jointHost.transform.parent = Selection.activeGameObject.transform;
		PathJointBehaviour jointController = jointHost.AddComponent<PathJointBehaviour> ();
		jointController.pathJoint = new PathJoint ();
		pathMapController.AddPathJointController (jointController);
		
		Selection.activeGameObject = jointHost;
		
	}
	
	[MenuItem ("Addons/Create Path Route")]
	public static void newPathRoute ()
	{
		PathJointBehaviour jointController = null;
		if (Selection.activeGameObject !=  null)
		{
			jointController = Selection.activeGameObject.GetComponent<PathJointBehaviour> ();
		}
		
		if (jointController ==  null)
		{
			Debug.LogError ("Please select a PathJoint first!");
			return;
		}
		
		GameObject routeHost = new GameObject ("routeHost");
		routeHost.transform.parent = Selection.activeGameObject.transform;
		PathRouteBehaviour routeController = routeHost.AddComponent<PathRouteBehaviour> ();
		routeController.pathRoute = new PathRoute ();
		jointController.pathJoint.AddRoute (routeController.pathRoute);
		
		Selection.activeGameObject = routeHost;
	}
	
	[MenuItem ("Addons/Create Bezier Path")]
	public static void newCurve ()
	{
		PathMapBehaviour pathMapController = null;
		if (Selection.activeGameObject != null)
		{
			pathMapController = Selection.activeGameObject.GetComponent<PathMapBehaviour> ();
		}
		
		if (pathMapController == null)
		{
			Debug.LogError ("Please select a PathMap first!");
			return;
		} 
		
		GameObject curveHost = new GameObject ("pathHost");
		curveHost.transform.parent = Selection.activeGameObject.transform;
		PathPairBehaviour pathController = curveHost.AddComponent<PathPairBehaviour> ();
		pathMapController.AddPathController (pathController);
		
		Selection.activeGameObject = curveHost;
	}
	
	[MenuItem ("Addons/Load Path Map")]
	public static void loadPathMap ()
	{
		// TODO load path map files
	}
	
	[MenuItem ("Addons/Deprecated/Create Bezier Path")]
	public static void deprecated_newpath ()
	{
		GameObject curveHost = new GameObject ("curveHost");
		curveHost.AddComponent<PathBehaviour> ();
	}
}
